package edu.gatech.asthma.ui_support;

import edu.gatech.asthma.R;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Adapter for the scheduler and calendar
 */
public class ScheduleAdapter extends BaseAdapter {
	private Context monthContext;
	private java.util.Calendar month;
	public GregorianCalendar prevmonth; 
	public GregorianCalendar prevMonthMaxSet;
	private GregorianCalendar selectedDate;
	int firstDay;
	int maxWeekNumber;
	int maxDayPrev;
	int calMaxDayPrev;
	int lastWeekDay;
	int leftDays;
	int monthLength;
	String itemValue, curentDateString;
	DateFormat df;

	private ArrayList<String> items;
	public static List<String> dayString;
	private View previousView;

	public ScheduleAdapter(Context c, GregorianCalendar monthCalendar) {
		ScheduleAdapter.dayString = new ArrayList<String>();
		 Locale.setDefault( Locale.US );
		month = monthCalendar;
		selectedDate = (GregorianCalendar) monthCalendar.clone();
		monthContext = c;
		month.set(GregorianCalendar.DAY_OF_MONTH, 1);
		this.items = new ArrayList<String>();
		df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
		curentDateString = df.format(selectedDate.getTime());
		refreshDays();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		TextView dayView;
		if (convertView == null) { 
			LayoutInflater vi = (LayoutInflater) monthContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.calendar_item, null);

		}
		dayView = (TextView) v.findViewById(R.id.date);
		
		String[] separatedTime = dayString.get(position).split("-"); // split daystring into parts
		String gridvalue = separatedTime[2].replaceFirst("^0*", ""); // get day from date
		// check if day is in current month
		if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
			// offdays
			dayView.setTextColor(Color.WHITE);
			dayView.setClickable(false);
			dayView.setFocusable(false);
		} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
			dayView.setTextColor(Color.WHITE);
			dayView.setClickable(false);
			dayView.setFocusable(false);
		} else {
			// current month
			dayView.setTextColor(Color.BLUE);
		}

		if (dayString.get(position).equals(curentDateString)) {
			setSelected(v);
			previousView = v;
		} else {
			v.setBackgroundResource(R.drawable.list_item_background);
		}
		dayView.setText(gridvalue);

		String date = dayString.get(position); // create date string

		if (date.length() == 1) {
			date = "0" + date;
		}
		String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
		if (monthStr.length() == 1) {
			monthStr = "0" + monthStr;
		}

		// show date icon if date is not empty and is in the items array
		ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
		if (date.length() > 0 && items != null && items.contains(date)) {
			iw.setVisibility(View.VISIBLE);
		} else {
			iw.setVisibility(View.INVISIBLE);
		}
		return v;
	}

	public View setSelected(View view) {
		if (previousView != null) {
			previousView.setBackgroundResource(R.drawable.list_item_background);
		}
		previousView = view;
		view.setBackgroundResource(R.drawable.calendar_cel_selectl);
		return view;
	}

	public void refreshDays() {
	
		items.clear();
		dayString.clear();
		Locale.setDefault( Locale.US );
		prevmonth = (GregorianCalendar) month.clone();
		firstDay = month.get(GregorianCalendar.DAY_OF_WEEK); // month start day
		maxWeekNumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH); // number of weeks in current month
		monthLength = maxWeekNumber * 7;
		maxDayPrev = getMaxDayPrev(); 
		calMaxDayPrev = maxDayPrev - (firstDay - 1);
		
		// A calendar instance for getting complete gridview, and the previous, current, and next months' dates
		prevMonthMaxSet = (GregorianCalendar) prevmonth.clone();
		prevMonthMaxSet.set(GregorianCalendar.DAY_OF_MONTH, calMaxDayPrev + 1);

		// fills calendar gridview
		for (int n = 0; n < monthLength; n++) {

			itemValue = df.format(prevMonthMaxSet.getTime());
			prevMonthMaxSet.add(GregorianCalendar.DATE, 1);
			dayString.add(itemValue);

		}
	}

	private int getMaxDayPrev() {
		int maxPrev;
		if (month.get(GregorianCalendar.MONTH) == month
				.getActualMinimum(GregorianCalendar.MONTH)) {
			prevmonth.set((month.get(GregorianCalendar.YEAR) - 1),
					month.getActualMaximum(GregorianCalendar.MONTH), 1);
		} else {
			prevmonth.set(GregorianCalendar.MONTH,
					month.get(GregorianCalendar.MONTH) - 1);
		}
		maxPrev = prevmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

		return maxPrev;
	}
	
	public void setItems(ArrayList<String> items) {
		for (int i = 0; i != items.size(); i++) {
			if (items.get(i).length() == 1) {
				items.set(i, "0" + items.get(i));
			}
		}
		this.items = items;
	}

	public int getCount() {
		return dayString.size();
	}

	public Object getItem(int position) {
		return dayString.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

}